package com.xiaoma.weblog.example.mybatisplus.config;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @ClassName MybatisPlusConfig
 * @Description:
 * @Author: 小马
 * @CreateDate: 2025/4/2 11:43
 * @Version: 1.0
 */
@Configuration
@MapperScan("com.xiaoma.weblog.example.mybatisplus.*")
public class MybatisPlusConfig {
    /**
     * 分页插件
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            // 获取参数方法
            Map<String, Object> paramMap = RequestDataHelper.getRequestData();
            if (CollectionUtils.isNotEmpty(paramMap)) {
                paramMap.forEach((k, v) -> System.err.println(k + "----" + v));
                // 获取传递的参数
                Long userId = (Long) paramMap.get("user_id");
                // 水平分表 3 张，对 ID 进行取模决定表名后缀
                String tableNameSuffix = "_" + userId % 3;
                // 组装动态表名
                return tableName + tableNameSuffix;
            }
            return tableName;
        });
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        // 3.4.3.2 作废该方式
        // dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
        return interceptor;
    }

    /**
     * 自定义批量插入 SQL 注入器
     */
    @Bean
    public InsertBatchSqlInjector insertBatchSqlInjector() {
        return new InsertBatchSqlInjector();
    }
}

//// 组装查询条件
//QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//// where age = 30
//queryWrapper.eq("age", 30);
//
//// 查询第 2 页数据，每页 10 条
//Page<User> page = new Page<>(2, 10);
//
//page = userMapper.selectPage(page, queryWrapper);
//System.out.println("总记录数：" + page.getTotal());
//        System.out.println("总共多少页：" + page.getPages());
//        System.out.println("当前页码：" + page.getCurrent());
//// 当前页数据
//List<User> users = page.getRecords();


//// 组装查询条件
//QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//// where age = 30
//queryWrapper.eq("age", 30);
//
//// 查询第 2 页数据，每页 10 条
//Page<User> page = new Page<>(2, 10);
//
//page = userService.page(page, queryWrapper);
//System.out.println("总记录数：" + page.getTotal());
//        System.out.println("总共多少页：" + page.getPages());
//        System.out.println("当前页码：" + page.getCurrent());
//// 当前页数据
//List<User> users = page.getRecords();